package leetcode;

/**
 * Created with IntelliJ IDEA.
 * User: Ghost
 * Date: 14-6-2
 * Time: 下午10:11
 * To change this template use File | Settings | File Templates.
 */
public class Best_Time_to_Buy_and_Sell_Stock {
    public int maxProfit(int[] prices) {
        if(prices==null || prices.length==0)
            return 0;
        int [] p = new int[prices.length];
        p[0] = 0;
        int max = 0;
        for(int i=1;i<prices.length;i++){
            if(p[i-1]+prices[i]-prices[i-1]>=0){
                p[i] = p[i-1]+prices[i]-prices[i-1];
            }
            else{
                p[i] = 0;
            }
            max = max>p[i]?max:p[i];
        }
        return max;
    }

    public static void main(String[] ags){
        Best_Time_to_Buy_and_Sell_Stock best_time_to_buy_and_sell_stock = new Best_Time_to_Buy_and_Sell_Stock();
        int[] p = {6,3,4,7,6,5,9};
        System.out.println(best_time_to_buy_and_sell_stock.maxProfit(p));
    }
}
